package com.pet.server.ext.action.square;

import net.sf.json.JSONObject;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.Result;
import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.data.PetMissing;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;

public class Recovered extends ActionBase{

	public Integer miss_id;
	public Recovered(){
		 needCheckAuth =true;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {
		miss_id=NumberUtil.getInteger(params.getFieldValue("miss_id"));
		if(StringUtil.isEmpty(miss_id)){
			throw ApiError.Type.INVALID_PARAM.toException();
		}	
	}
	@Override
	public Result invokeService() throws ApiError {
		UserService userService = new UserService();
		PetMissing miss = userService.findPetMissing(miss_id);
		if(null==miss||!StringUtil.equals(miss.user_id, user.user_id)||StringUtil.equals(miss.state,BaseAttribute.LOGIC_TRUE)){
			throw ApiError.Type.LOGIC_ERROR.toException();
		}
		userService.updateMissingState(miss_id);
		JSONObject o = new JSONObject();
		o.accumulate("message", "宠物已领回!");
		Result result = new Result();
		result.setRet(o);
	    return result;
	}
}
